package com.fc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.redis.core.query.QueryUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fc.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.fc.entity.SysPermission;
import com.fc.service.SysPermissionService;


import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author zffc
 * @since 2023-07-18
 */
@Controller
@RequestMapping("/sysPermission")
public class SysPermissionController {


    @Autowired
    private SysPermissionService sysPermissionService;

    @GetMapping("/toIndex")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        //因为配置了自带的视图解析器，所以默认访问template目录
        mv.setViewName("/sysPermission/list");
        return mv;
    }

    @GetMapping("/toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/sysPermission/form");
        return mv;
    }

    @GetMapping("/toEdit/{id}")
    public ModelAndView toEdit(@PathVariable String id) {//路径参数
        ModelAndView mv = new ModelAndView();
        SysPermission sysPermission = sysPermissionService.getById(id);
        List<SysPermission> permissions = sysPermissionService.list();
        mv.setViewName("/sysPermission/form");
        mv.addObject("sysPermission", sysPermission);
        mv.addObject("permissions",permissions);
        return mv;
    }


    /**
     * @param sysPermission
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public Result<?> list(SysPermission sysPermission,
                          @RequestParam(name = "pageNumber", defaultValue = "1") Integer pageNumber,
                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysPermission> page = new Page(pageNumber, pageSize);
        return Result.OK(sysPermissionService.page(page));
    }


    /**
     * 添加或修改方法
     *
     * @param sysPermission
     * @return
     */
    @PostMapping("/addOrEdit")
    @ResponseBody
    public Result<?> addOrEdit(@RequestBody SysPermission sysPermission) {
        sysPermissionService.saveOrUpdate(sysPermission);
        return Result.OK("ok");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @ResponseBody
    public Result<?> delete(@PathVariable String id) {
        sysPermissionService.removeById(id);
        return Result.OK("ok");
    }


}

